#include "queue_demo.h"

#define USE_COPY	0

UINT32 tid_s;
UINT32 tid_r1, tid_r2;

UINT32 queue;

UINT8 temp_buf[50];

#if USE_COPY
static void send_entry(VOID){
    UINT8 i=0;
    UINT8 sbuf[10] = "hello ";
	LOS_TaskDelay(5000);
	// 执行5次
    while(i<5){
        sbuf[6] = i + '0';
		// 将数据写入队列
        LOS_QueueWriteCopy(queue, sbuf, 10, 0);
        printf("write buf %s\n", sbuf);
        i++;
        LOS_TaskDelay(1000);
    }

    return;
}

static void recv1_entry(VOID){
    UINT32 ret;
    UINT32 rsize=10;
    UINT8 rbuf[10]={0};
    while(1)
    {
		// 读取数据
        ret = LOS_QueueReadCopy(queue, rbuf, &rsize, 100);
        if(ret == LOS_OK){ printf("recv1_task get %d bytes, content is %s\n", rsize, rbuf); }
        else { printf("recv1_task read err, errno is %x\n", ret); }
        LOS_TaskDelay(2500);
    }
    return;
}
static void recv2_entry(VOID){
    UINT32 ret;
    UINT32 rsize=10;
    UINT8 rbuf[10]={0};
    while(1)
    {
		// 读取数据
        ret = LOS_QueueReadCopy(queue, rbuf, &rsize, 100);
        if(ret == LOS_OK){ printf("recv2_task get %d bytes, content is %s\n", rsize, rbuf); }
        else { printf("recv2_task read err, errno is %x\n", ret); }
        LOS_TaskDelay(3000);
    }
    return;
}
#else
static void send_entry(VOID){
    UINT8 i=0;
    UINT8 sbuf[10] = "hello ";

	LOS_TaskDelay(5000);
	// 执行5次
    while(i<5){
        sbuf[6] = i + '0';
		// 将sbuf复制到temp_buf
		memcpy(temp_buf+i*10, sbuf, 10);
		// 将数据写入队列,此时写入的其实是缓冲区的首地址
        LOS_QueueWrite(queue, temp_buf+i*10, 4, 0xffff);
        printf("write buf %s\n", sbuf);
		i++;
        LOS_TaskDelay(1000);
    }

    return;
}

static void recv1_entry(VOID){
    UINT32 ret;
    UINT32 raddr;
    while(1)
    {
		// 读取数据,此时读到的是一个地址
        ret = LOS_QueueRead(queue, &raddr, 4, 100);
        if(ret == LOS_OK){ printf("recv1_task get data, content is: %s\n", (UINT8 *)raddr); }
        else { printf("recv1_task read err, errno is %x\n", ret); }
        LOS_TaskDelay(2500);
    }
    return;
}
static void recv2_entry(VOID){
    UINT32 ret;
    UINT32 raddr;
    while(1)
    {
		// 读取数据,此时读到的是一个地址
        ret = LOS_QueueRead(queue, &raddr, 4, 100);
        if(ret == LOS_OK){ printf("recv2_task get data, content is: %s\n", (UINT8 *)raddr); }
        else { printf("recv2_task read err, errno is %x\n", ret); }
        LOS_TaskDelay(3000);
    }
    return;
}
#endif
static UINT32 my_task(UINT32 *tid, char *name, UINT16 pri, UINT32 stack_size, TSK_ENTRY_FUNC func) {
    UINT32 ret;
    TSK_INIT_PARAM_S param={0};

    // 锁住任务调度，防止高优先级任务调度
    LOS_TaskLock();
    // 任务名字
    param.pcName = name;
    // 任务入口地址
    param.pfnTaskEntry = func;
    // 任务优先级
    param.usTaskPrio = pri;
    // 任务栈
    param.uwStackSize = stack_size;
    // 调用系统函数，创建任务。成功之后任务处于就绪状态
    ret = LOS_TaskCreate(&tid, &param);
    //解锁任务调度
    LOS_TaskUnlock();

    return ret;
}

UINT32 queue_entry(){
	UINT32 ret;
#if USE_COPY
	// 创建队列,长度5,节点大小10byte
   	ret = LOS_QueueCreate("QUEUE", 5, &queue, 0, 10);
    if(ret != LOS_OK){ printf("create create queue failed\n"); return -1; }
	printf("Use LOS_QueueReadCopy/LOS_QueueWriteCopy interface\n");
#else
	// 创建队列,长度5,节点大小4byte
	// 不使用copy类型的读写，此时队列存储的是一个地址，4字节足够
   	ret = LOS_QueueCreate("QUEUE", 5, &queue, 0, 4);
    if(ret != LOS_OK){ printf("create create queue failed\n"); return -1; }
    printf("Use LOS_QueueRead/LOS_QueueWrite interface\n");
#endif
	// 创建发送任务
	ret = my_task(&tid_s, "sender", 9, 0x2000, send_entry);
	if(ret != LOS_OK){ printf("create sender failed\n"); return -1; }
	// 创建接收任务
	ret = my_task(&tid_r1, "recver1", 9, 0x2000, recv1_entry);
	if(ret != LOS_OK){ printf("create recver1 failed\n"); return -1; }
	ret = my_task(&tid_r2, "recver2", 9, 0x2000, recv2_entry);
	if(ret != LOS_OK){ printf("create recver2 failed\n"); return -1; }

	return ret;
}
SYS_RUN(queue_entry);